package com.chonor.mall.service.impl;

import com.chonor.mall.dao.CouponDao;
import com.chonor.mall.dao.CouponProductCategoryDao;
import com.chonor.mall.dao.CouponProductDao;
import com.chonor.mall.dto.CouponParam;
import com.chonor.mall.mapper.*;
import com.chonor.mall.model.*;
import com.chonor.mall.service.CouponService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class CouponServiceImpl implements CouponService {

    @Autowired
    private CouponMapper couponMapper;

    @Autowired
    private CouponDao couponDao;

    @Autowired
    private CouponProductDao couponProductDao;

    @Autowired
    private CouponProductMapper couponProductMapper;

    @Autowired
    private CouponProductCategoryDao couponProductCategoryDao;

    @Autowired
    private CouponProductCategoryMapper couponProductCategoryMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    @Override
    public int create(CouponParam couponParam) {
        couponParam.setCount(couponParam.getPublishCount());
        couponParam.setUseCount(0);
        couponParam.setReceiveCount(0);
        //插入优惠券表
        int count = couponMapper.insert(couponParam);
        //插入优惠券和商品关系表
        if(couponParam.getUseType().equals(2)){
            for(CouponProduct productRelation:couponParam.getProductRelationList()){
                productRelation.setCouponId(couponParam.getId());
            }
            couponProductDao.insertList(couponParam.getProductRelationList());
        }
        //插入优惠券和商品分类关系表
        if(couponParam.getUseType().equals(1)){
            for (CouponProductCategory couponProductCategoryRelation : couponParam.getProductCategoryRelationList()) {
                couponProductCategoryRelation.setCouponId(couponParam.getId());
            }
            couponProductCategoryDao.insertList(couponParam.getProductCategoryRelationList());
        }
        return count;
    }

    @Override
    public int create(Coupon coupon) {
        coupon.setUseCount(0);
        coupon.setReceiveCount(0);
        int count = couponMapper.insert(coupon);
        return count;
    }

    @Override
    public int delete(Long id) {
        //删除优惠券
        int count = couponMapper.deleteByPrimaryKey(id);
        //删除商品关联
        deleteProductRelation(id);
        //删除商品分类关联
        deleteProductCategoryRelation(id);
        return count;
    }

    private void deleteProductCategoryRelation(Long id) {
        CouponProductCategoryExample productCategoryRelationExample = new CouponProductCategoryExample();
        productCategoryRelationExample.createCriteria().andCouponIdEqualTo(id);
        couponProductCategoryMapper.deleteByExample(productCategoryRelationExample);
    }

    private void deleteProductRelation(Long id) {
        CouponProductExample productRelationExample = new CouponProductExample();
        productRelationExample.createCriteria().andCouponIdEqualTo(id);
        couponProductMapper.deleteByExample(productRelationExample);
    }

    @Override
    public int update(Long id, CouponParam couponParam) {
        couponParam.setId(id);
        int count =couponMapper.updateByPrimaryKey(couponParam);
        //删除后插入优惠券和商品关系表
        if(couponParam.getUseType().equals(2)){
            for(CouponProduct productRelation:couponParam.getProductRelationList()){
                productRelation.setCouponId(couponParam.getId());
            }
            deleteProductRelation(id);
            couponProductDao.insertList(couponParam.getProductRelationList());
        }
        //删除后插入优惠券和商品分类关系表
        if(couponParam.getUseType().equals(1)){
            for (CouponProductCategory couponProductCategoryRelation : couponParam.getProductCategoryRelationList()) {
                couponProductCategoryRelation.setCouponId(couponParam.getId());
            }
            deleteProductCategoryRelation(id);
            couponProductCategoryDao.insertList(couponParam.getProductCategoryRelationList());
        }
        return count;
    }

    @Override
    public int update(Long id, Coupon coupon) {
        coupon.setId(id);
        int count =couponMapper.updateByPrimaryKey(coupon);
        return count;
    }

    @Override
    public List<Coupon> list(String name, Integer type, Integer pageSize, Integer pageNum) {
        CouponExample example = new CouponExample();
        CouponExample.Criteria criteria = example.createCriteria();
        if(!StringUtils.isEmpty(name)){
            criteria.andNameLike("%"+name+"%");
        }
        if(type!=null){
            criteria.andTypeEqualTo(type);
        }
        PageHelper.startPage(pageNum,pageSize);
        return couponMapper.selectByExample(example);
    }

    @Override
    public CouponParam getItem(Long id) {
        return couponDao.getItem(id);
    }

    @Override
    public int setProduct(Long id, List<Long> ids) {
        //校验优惠券的类型

        //查询商品
        ProductExample example = new ProductExample();
        example.createCriteria().andIdIn(ids);
        List<Product> products = productMapper.selectByExample(example);
        //设置
        List<CouponProduct> couponProducts = new ArrayList<>(products.size());
        for (int i=0 ; i < products.size() ; i++) {
            couponProducts.get(i).setCouponId(id);
            couponProducts.get(i).setProductId(products.get(i).getId());
            couponProducts.get(i).setProductName(products.get(i).getName());
            couponProducts.get(i).setProductSn(products.get(i).getCode());
        }
        int count = couponProductDao.insertList(couponProducts);
        return count;
    }

    @Override
    public int setProductCategory(Long id, List<Long> ids) {
        //校验

        //查询商品分类
        ProductCategoryExample example = new ProductCategoryExample();
        example.createCriteria().andIdIn(ids);
        List<ProductCategory> productCategories = productCategoryMapper.selectByExample(example);
        //设置
        List<CouponProductCategory> couponProductCategories = new ArrayList<>(productCategories.size());
        for (int i=0 ; i < productCategories.size() ; i++) {
            couponProductCategories.get(i).setCouponId(id);
            couponProductCategories.get(i).setProductCategoryId(productCategories.get(i).getId());
            couponProductCategories.get(i).setParentCategoryName(productCategories.get(i).getName());
        }
        int count = couponProductCategoryDao.insertList(couponProductCategories);
        return count;
    }


}
